package cn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.GridCommon;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.sys.MenuEntity;
import cn.kpi.entity.sys.RoleEntity;
import cn.kpi.service.sys.IMenuService;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController  {
	@Autowired
	private IMenuService iMenuService;
	
	@RequestMapping(value = "/menu_list")
	public String RoleList(Model model) {
		try{
			List<MenuEntity> lst = iMenuService.GetMenuList();
			model.addAttribute("lst", lst);
		}
		catch(Exception ex){
			
		}
		return "sys/menu_list";
	}
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<MenuEntity> GetList(HttpServletRequest request) {
		List<MenuEntity> lst=null;
		LayTable<MenuEntity> layTable=new LayTable<MenuEntity>();
		try {
			String RoleName = request.getParameter("rolename");
			
			lst = iMenuService.GetMenuList();
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}

			layTable.setCode(0);
			layTable.setCount(100);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	@RequestMapping(value = "/add_menu")
	public String AddMenu(HttpServletRequest request,HttpServletResponse response){
		try{
			MenuEntity entity=new MenuEntity();
			entity.setMenuid(Long.parseLong(request.getParameter("menuid")));
			entity.setParentid(Long.parseLong(request.getParameter("parentid")));
			entity.setRemark(request.getParameter("remark"));
			entity.setMenuname(request.getParameter("menuname"));
			entity.setIcon(request.getParameter("icon"));
			entity.setUrl(request.getParameter("url"));
			entity.setMenutype(Integer.parseInt(request.getParameter("menutype")));
			
			
			int iResult=0;
			iResult=iMenuService.InsertEntityProc(entity);	
			
			return renderStringScript(response,"<script>window.parent.CloseDialog("+iResult+")</script>");
		}
		catch(Exception ex){
			return renderStringScript(response,"<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete_menu")
	public ExceResult DeleteMenu(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long menuid=Long.parseLong(request.getParameter("menuid"));
		 int iResult=iMenuService.DeleteEntity(menuid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}
}
